<?php

namespace App\Service;

use App\Model\ActiveModel;
use App\Model\CommunityDynamicCommentModel;
use App\Model\CommunityDynamicGoodModel;
use App\Model\CommunityDynamicModel;
use App\Model\CommunityDynamicSeeModel;
use App\Model\CommunityTopicModel;
use App\Model\ConfigNavModel;
use App\Model\NewsCommentModel;
use App\Model\NewsGoodModel;
use App\Model\NewsModel;
use App\Model\UserModel;
use App\Utility\DataConversion;
use EasySwoole\ORM\Exception\Exception;
use Throwable;

class CommunityService extends BaseService
{
    /**
     * @param $limit
     * @param $page
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getTopic($limit, $page): array
    {
        $static = ConfigNavModel::create()->getConfig(['method' => 'community']);
        if (!$static) {
            return [
                'result' => false,
                'msg' => '该模块已关闭'
            ];
        }
        $model = CommunityTopicModel::create();
        $model->limit($limit * ($page - 1), $limit)->withTotalCount();
        $list = $model->order("id", "DESC")->all(['is_deleted' => 0]);
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();

        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无更多话题'
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $list
        ];
    }

    /**
     * @param $tid
     * @param $sort
     * @param $page
     * @param $limit
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getDynamic($tid, $sort, $page, $limit): array
    {
        $where = ['is_deleted' => 0];
        $model = CommunityDynamicModel::create();
        $model->limit($limit * ($page - 1), $limit)->withTotalCount();
        if ($tid != 99) {
            $where['topic'] = $tid;
        }
        if ($sort == 0) {
            $model->order('`like`', 'DESC')->order('id', "DESC");
        } else {
            $model->order('id', "DESC");
        }
        $list = $model->where($where)->all();
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();
        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($list as $item) {
            $user = UserModel::create()->getUser(['id' => $item['uid']]);
            $data[] = [
                'id' => $item['id'],
                'avatar' => $user->avatar ?? '',
                'username' => $user->username ?? '匿名',
                'from' => $item['from'],
                'theme' => ConfigService::getInstance()->getTopic($item['topic']),
                'content' => $item['content'],
                'see' => $item['see'],
                'like' => $item['like'],
                'comment' => $item['comment'],
                'images' => json_decode($item['images'], true),
                'time' => DataConversion::getInstance()->timeToDate($item['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $sort
     * @param $page
     * @param $limit
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getCommunity($sort, $page, $limit): array
    {
        $where = ['is_deleted' => 0];
        $model = CommunityDynamicModel::create();
        $model->limit($limit * ($page - 1), $limit)->withTotalCount();
        if ($sort == 0) {
            $model->order('`like`', 'DESC')->order('id', "DESC");
            $active = ActiveModel::create()->limit($page - 1, 1)->order('is_hot', 'DESC')->order('id', 'DESC')->all();
        } else {
            $model->order('id', "DESC");
            $active = ActiveModel::create()->limit($page - 1, 1)->order('id', 'DESC')->all();
        }
        $list = $model->where($where)->all();
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();
        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $time = time();
        $data = [];
        if (isset($active[0]) && !empty($active[0])) {
            $active = $active[0];
            $data[] = [
                'type' => 1,
                'id' => $active['id'],
                'title' => $active['title'],
                'free' => $active['type'],
                'price' => $active['price'],
                'join' => $active['join'],
                'limit' => $active['person'],
                'image' => $active['image'],
                'is_hot' => $active['is_hot'],
                'sponsor' => $active['sponsor'],
                'content' => $active['content'],
                'matter' => $active['matter'],
                'address' => $active['address'],
                'contact' => $active['contact'],
                'time' => date("Y-m-d H:i:s", $active['time']),
                'end_time' => date("Y-m-d H:i:s", $active['end_time']),
                'create_time' => date("Y-m-d H:i:s", $active['create_time']),
                'see' => $active['see'],
                'status' => $active['end_time'] < $time ? -1 : 0
            ];
        }
        foreach ($list as $item) {

            $user = UserModel::create()->getUser(['id' => $item['uid']]);
            $data[] = [
                'type' => 0,
                'id' => $item['id'],
                'avatar' => $user->avatar ?? '',
                'username' => $user->username ?? '匿名',
                'from' => $item['from'],
                'theme' => ConfigService::getInstance()->getTopic($item['topic']),
                'content' => $item['content'],
                'see' => $item['see'],
                'like' => $item['like'],
                'comment' => $item['comment'],
                'images' => json_decode($item['images'], true),
                'time' => DataConversion::getInstance()->timeToDate($item['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $topic
     * @param $from
     * @param $content
     * @param $images
     * @param $location
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function release($uid, $topic, $from, $content, $images, $location): array
    {
        $par = [
            'uid' => $uid,
            'topic' => $topic,
            'from' => $from,
            'content' => $content,
            'images' => json_encode($images),
            'location' => $location
        ];

        $res = CommunityDynamicModel::create()->addDynamic($par);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '发布失败'
            ];
        }

        return [
            'result' => true,
            'msg' => '发布成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $uid
     * @param $limit
     * @param $page
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getMyRelease($uid, $limit, $page): array
    {
        $where = ['is_deleted' => 0, 'uid' => $uid];
        $model = CommunityDynamicModel::create();
        $model->limit($limit * ($page - 1), $limit)->withTotalCount();

        $model->order('id', "DESC");
        $list = $model->where($where)->all();
        $result = $model->lastQueryResult();
        // 总条数
        $total = $result->getTotalCount();
        if (empty($list)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($list as $item) {
            $user = UserModel::create()->getUser(['id' => $item['uid']]);
            $data[] = [
                'id' => $item['id'],
                'avatar' => $user->avatar ?? '',
                'username' => $user->username ?? '匿名',
                'from' => $item['from'],
                'theme' => ConfigService::getInstance()->getTopic($item['topic']),
                'content' => $item['content'],
                'see' => $item['see'],
                'like' => $item['like'],
                'comment' => $item['comment'],
                'images' => json_decode($item['images'], true),
                'time' => DataConversion::getInstance()->timeToDate($item['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $gid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function likeDynamic($uid, $gid): array
    {
        $like = CommunityDynamicGoodModel::create()->getDynamic(['gid' => $gid, 'uid' => $uid]);
        if ($like) {
            return [
                'result' => false,
                'msg' => '已点赞,请勿重复操作'
            ];
        }

        $param = [
            'uid' => $uid,
            'gid' => $gid,
            'like' => 1
        ];

        $res = CommunityDynamicGoodModel::create()->addDynamic($param);

        $res1 = $this->see($uid, $gid);
        if (!$res || !$res1) {
            return [
                'result' => false,
                'msg' => '操作失败'
            ];
        }

        return [
            'result' => true,
            'msg' => '已点赞',
            'data' => $res
        ];
    }

    /**
     * @param $uid
     * @param $gid
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function see($uid, $gid): bool
    {
        $param = [
            'uid' => $uid,
            'gid' => $gid,
            'see' => 1
        ];

        $res = CommunityDynamicSeeModel::create()->addDynamic($param);
        if (!$res) {
            return false;
        }
        return true;
    }

    /**
     * @param $uid
     * @param $gid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getDynamicDetail($uid, $gid): array
    {
        $dynamic = CommunityDynamicModel::create()->getDynamic(['id' => $gid]);
        if (!$dynamic) {
            return [
                'result' => false,
                'msg' => '该动态已删除'
            ];
        }
        if ($uid != $dynamic->uid) {
            $this->see($uid, $gid);
        }

        $user = UserModel::create()->getUser(['id' => $dynamic->uid]);
        if (!$user) {
            return [
                'result' => false,
                'msg' => '该账户已注销'
            ];
        }
        $data = [
            'id' => $gid,
            'avatar' => $user->avatar,
            'username' => $user->username,
            'date' => DataConversion::getInstance()->timeToDate($dynamic->create_time),
            'content' => $dynamic->content,
            'images' => json_decode($dynamic->images, true),
            "see" => $dynamic->see,
            "comment" => $dynamic->comment,
            'like' => $dynamic->like,
            'location' => $dynamic->location,
            'from' => $dynamic->from,
            'theme' => ConfigService::getInstance()->getTopic($dynamic->topic),
            'time' => DataConversion::getInstance()->timeToDate($dynamic->create_time),
        ];

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $gid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getDynamicComment($uid, $gid): array
    {
        $comments = CommunityDynamicCommentModel::create()->where(['gid' => $gid, 'nid' => 0, 'is_deleted' => 0])->all();
        if (empty($comments)) {
            return [
                'result' => false,
                'msg' => '暂无评论'
            ];
        }
        $data = [];
        foreach ($comments as $comment) {
            $user = UserModel::create()->getUser(['id' => $comment['uid']]);
            $data[] = [
                'id' => $comment['id'],
                'username' => $user->username,
                'avatar' => $user->avatar ?? '',
                'time' => DataConversion::getInstance()->timeToDate($comment['create_time']),
                'likeNum' => $comment['likeNum'],
                'content' => $comment['content'],
                'isLike' => false,
                'reply' => $this->comment_filter($comment['id']),
                'allReply' => count($this->comment_filter($comment['id']))
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => [
                'total' => count($data),
                'data' => $data
            ]
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function comment_filter($id): array
    {
        $comments = CommunityDynamicCommentModel::create()->all(['nid' => $id, 'is_deleted' => 0]);
        if (!is_array($comments) || empty($comments)) return [];
        $data = [];
        foreach ($comments as $comment) {
            $user = UserModel::create()->getUser(['id' => $comment['uid']]);
            $data[] = [
                'id' => $comment['id'],
                'username' => $user->username ?? '匿名用户',
                'avatar' => $user->avatar ?? '',
                'time' => DataConversion::getInstance()->timeToDate($comment['create_time']),
                'likeNum' => $comment['likeNum'],
                'content' => $comment['content'],
                'isLike' => false,
                'reply' => $this->comment_filter($comment['id']),
                'allReply' => count($this->comment_filter($comment['id']))
            ];
        }

        return $data;
    }

    /**
     * @param $uid
     * @param $id
     * @param $gid
     * @param $content
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function commentDynamic($uid, $id, $gid, $content): array
    {
        $param = [
            'uid' => $uid,
            'gid' => $gid,
            'content' => $content
        ];
        if ($id != 0) {
            $comment = CommunityDynamicCommentModel::create()->getDynamic(['id' => $id]);
            if ($comment) {
                $param['nid'] = $id;
                $param['ruid'] = $comment->uid;
            }
        } else {
            $comment = CommunityDynamicModel::create()->getDynamic(['id' => $gid]);
            $param['ruid'] = $comment->uid;
        }

        $res = CommunityDynamicCommentModel::create()->addDynamic($param);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '评论失败'
            ];
        }
        $data = $this->getDynamicComment($uid, $gid);
        return [
            'result' => true,
            'msg' => '评论成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getNews($uid, $type): array
    {
        $where = ['is_deleted' => 0];
        if ($type != 99) {
            $where['type'] = $type;
        }
        $banners = NewsModel::create()->where($where)->order('id', 'DESC')->all();

        if (empty($banners)) {
            return [
                'result' => false,
                'msg' => '获取失败'
            ];
        }
        $data = [];
        foreach ($banners as $banner) {
            $banner['time'] = DataConversion::getInstance()->timeToDate($banner['create_time']);
            $banner['label'] = mb_substr($banner['content'], 0, 32);
            $data[] = $banner;
        }
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getNewDetail($uid, $id): array
    {
        $news = NewsModel::create()->getNews(['id' => $id]);
        $data = $news->toArray();
        $data['time'] = DataConversion::getInstance()->timeToDate($news['create_time']);

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function delDynamic($uid, $id): array
    {
        $res = CommunityDynamicModel::create()->update(['is_deleted' => 1], ['uid' => $uid, 'id' => $id]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '操作失败'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $uid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getMyComment($uid): array
    {
        $comment = CommunityDynamicCommentModel::create()->where(['uid' => $uid, 'is_deleted' => 0])->all();
        if (empty($comment)) {
            return [
                'result' => false,
                'msg' => '暂无评论'
            ];
        }
        $ids = array_column($comment, 'gid');
        $dynamic = CommunityDynamicModel::create()->where("id", $ids, 'IN')->where(['is_deleted' => 0])->order('id', 'DESC')->all();
        $data = [];
        foreach ($dynamic as $item) {
            $user = UserModel::create()->getUser(['id' => $item['uid']]);
            $data[] = [
                'id' => $item['id'],
                'avatar' => $user->avatar ?? '',
                'username' => $user->username ?? '匿名',
                'from' => $item['from'],
                'theme' => ConfigService::getInstance()->getTopic($item['topic']),
                'content' => $item['content'],
                'see' => $item['see'],
                'like' => $item['like'],
                'comment' => $item['comment'],
                'images' => json_decode($item['images'], true),
                'time' => DataConversion::getInstance()->timeToDate($item['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getMyLike($uid): array
    {
        $like = CommunityDynamicGoodModel::create()->where(['uid' => $uid, 'is_deleted' => 0])->all();
        if (empty($like)) {
            return [
                'result' => false,
                'msg' => '暂无点赞'
            ];
        }
        $ids = array_column($like, 'gid');
        $dynamic = CommunityDynamicModel::create()->where("id", $ids, 'IN')->where(['is_deleted' => 0])->order('id', 'DESC')->all();
        $data = [];
        foreach ($dynamic as $item) {
            $user = UserModel::create()->getUser(['id' => $item['uid']]);
            $data[] = [
                'id' => $item['id'],
                'avatar' => $user->avatar ?? '',
                'username' => $user->username ?? '匿名',
                'from' => $item['from'],
                'theme' => ConfigService::getInstance()->getTopic($item['topic']),
                'content' => $item['content'],
                'see' => $item['see'],
                'like' => $item['like'],
                'comment' => $item['comment'],
                'images' => json_decode($item['images'], true),
                'time' => DataConversion::getInstance()->timeToDate($item['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function forwardDynamic($uid, $id): array
    {
        $dynamic = CommunityDynamicModel::create()->getDynamic(['id' => $id]);
        if (!$dynamic) {
            return [
                'result' => false,
                'msg' => '该动态不存在'
            ];
        }

        $u = UserModel::create()->getUser(['id' => $dynamic->uid]);
        $from = '转发自 ' . $u->username . ' 的动态';
        $par = [
            'uid' => $uid,
            'topic' => $dynamic->topic,
            'from' => $from,
            'content' => $dynamic->content,
            'location' => $dynamic->location,
            'images' => $dynamic->images
        ];

        $res = CommunityDynamicModel::create()->addDynamic($par);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '转发失败'
            ];
        }
        return [
            'result' => true,
            'msg' => '转发成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $uid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getMyForward($uid): array
    {
        $dynamic = CommunityDynamicModel::create()->where(['uid' => $uid, 'is_deleted' => 0])->where('from', '转发%', 'LIKE')->order('id', 'DESC')->all();
        if (!$dynamic) {
            return [
                'result' => false,
                'msg' => '暂无动态'
            ];
        }

        $data = [];
        foreach ($dynamic as $item) {
            $user = UserModel::create()->getUser(['id' => $item['uid']]);
            $data[] = [
                'id' => $item['id'],
                'avatar' => $user->avatar ?? '',
                'username' => $user->username ?? '匿名',
                'from' => $item['from'],
                'theme' => ConfigService::getInstance()->getTopic($item['topic']),
                'content' => $item['content'],
                'see' => $item['see'],
                'like' => $item['like'],
                'comment' => $item['comment'],
                'images' => json_decode($item['images'], true),
                'time' => DataConversion::getInstance()->timeToDate($item['create_time'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $gid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function newsLike($uid, $gid): array
    {
        $like = NewsGoodModel::create()->getDynamic(['gid' => $gid, 'uid' => $uid]);
        if ($like) {
            return [
                'result' => false,
                'msg' => '已点赞,请勿重复操作'
            ];
        }

        $param = [
            'uid' => $uid,
            'gid' => $gid,
            'like' => 1
        ];

        $res = NewsGoodModel::create()->addDynamic($param);

        if (!$res) {
            return [
                'result' => false,
                'msg' => '操作失败'
            ];
        }

        return [
            'result' => true,
            'msg' => '已点赞',
            'data' => $res
        ];
    }

    /**
     * @param $uid
     * @param $id
     * @param $gid
     * @param $content
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function newsComment($uid, $id, $gid, $content): array
    {
        $param = [
            'uid' => $uid,
            'gid' => $gid,
            'content' => $content
        ];
        if ($id != 0) {
            $comment = NewsCommentModel::create()->getDynamic(['id' => $id]);
            if ($comment) {
                $param['nid'] = $id;
            }
        }

        $res = NewsCommentModel::create()->addDynamic($param);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '评论失败'
            ];
        }
        $data = $this->getNewsComment($uid, $gid);
        return [
            'result' => true,
            'msg' => '评论成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $gid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getNewsComment($uid, $gid): array
    {
        $comments = NewsCommentModel::create()->where(['gid' => $gid, 'nid' => 0, 'is_deleted' => 0])->all();
        if (empty($comments)) {
            return [
                'result' => false,
                'msg' => '暂无评论'
            ];
        }
        $data = [];
        foreach ($comments as $comment) {
            $user = UserModel::create()->getUser(['id' => $comment['uid']]);
            $data[] = [
                'id' => $comment['id'],
                'username' => $user->username ?? '匿名用户',
                'avatar' => $user->avatar ?? '',
                'time' => DataConversion::getInstance()->timeToDate($comment['create_time']),
                'likeNum' => $comment['likeNum'],
                'content' => $comment['content'],
                'isLike' => false,
                'reply' => $this->news_comment_filter($comment['id']),
                'allReply' => count($this->news_comment_filter($comment['id']))
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => [
                'total' => count($data),
                'data' => $data
            ]
        ];
    }

    /**
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function news_comment_filter($id): array
    {
        $comments = NewsCommentModel::create()->all(['nid' => $id, 'is_deleted' => 0]);
        if (!is_array($comments) || empty($comments)) return [];
        $data = [];
        foreach ($comments as $comment) {
            $user = UserModel::create()->getUser(['id' => $comment['uid']]);
            $data[] = [
                'id' => $comment['id'],
                'username' => $user->username ?? '匿名用户',
                'avatar' => $user->avatar ?? '',
                'time' => DataConversion::getInstance()->timeToDate($comment['create_time']),
                'likeNum' => $comment['likeNum'],
                'content' => $comment['content'],
                'isLike' => false,
                'reply' => $this->comment_filter($comment['id']),
                'allReply' => count($this->comment_filter($comment['id']))
            ];
        }

        return $data;
    }

    /**
     * @param $uid
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getUnRead($uid): array
    {
        $comments = CommunityDynamicCommentModel::create()->where(['ruid' => $uid, 'is_read' => 0, 'is_deleted' => 0])->count();

        if ($comments == 0) {
            return [
                'result' => false,
                'msg' => '暂无评论'
            ];
        }

        return [
            'result' => true,
            'msg' => '您有' . $comments . '条未读动态',
            'data' => $comments
        ];
    }

    /**
     * @param $uid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getUnReadDetail($uid): array
    {
        $comments = CommunityDynamicCommentModel::create()->where(['ruid' => $uid, 'is_read' => 0, 'is_deleted' => 0])->all();
        if (empty($comments)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($comments as $comment) {
            $user = UserModel::create()->getUser(['id' => $comment['uid']]);
            if ($user) {
                $par = [
                    'id' => $comment['id'],
                    'avatar' => $user->avatar,
                    'username' => $user->username,
                    'gid' => $comment['gid'],
                    'time' => DataConversion::getInstance()->timeToDate($user->create_time),
                    'content' => $comment['content']
                ];
                $data[] = $par;
            }
        }

        $res = CommunityDynamicCommentModel::create()->where(['ruid' => $uid, 'is_read' => 0, 'is_deleted' => 0])->update(['is_read' => 1]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '系统错误'
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }
}